import os 
import time
import requests

from os import getenv
from bs4 import BeautifulSoup
from flask import Flask, render_template, make_response
from dotenv import load_dotenv

load_dotenv()

app = Flask(__name__)

url = getenv('URL')
usr = getenv('USER')
pwd = getenv('PWD')

def get_status():
    with requests.get(url, auth=(usr, pwd)) as response:
        response.raise_for_status()
        soup = BeautifulSoup(response.content, 'html.parser')
        return [soup.find('td',{'id':f'l{i}_line_state'}).get_text() for i in range(1,9)]
    
@app.route("/")
def index():
    status = get_status()
    response = make_response(render_template('index.html', status=status))
    response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
    return response

@app.route("/update")
def update():
    return {'status': get_status()}

if __name__ == '__main__':
    app.run(host='0.0.0.0', port='7777')